import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/organization'

const api = {

    /**
     * 新增组织结构信息
     * @param organization
     * @returns {Promise<*|undefined>}
     */
    createOrganization: (organization) =>
      axiosClient().post(`${requestUrl}/addInfo`, organization),

    /**
     * 根据ID删除组织结构信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteOrganizationById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新组织结构信息
     * @param id
     * @param organization
     * @returns {Promise<*|undefined>}
     */
    updateOrganizationFull: (id, organization) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, organization),

    /**
     * 根据ID部分更新组织结构信息
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateOrganizationPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询组织结构信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getOrganizationById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询组织结构信息
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getOrganizationPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询组织结构信息
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getOrganizationList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.orgLevelId) {
            params.append('orgLevelId', businessParam.orgLevelId)
        }
        if (businessParam.levelPath) {
            params.append('levelPath', businessParam.levelPath)
        }
        if (businessParam.orgCode) {
            params.append('orgCode', businessParam.orgCode)
        }
        if (businessParam.orgName) {
            params.append('orgName', businessParam.orgName)
        }
        if (businessParam.orgShortName) {
            params.append('orgShortName', businessParam.orgShortName)
        }
        if (businessParam.orgCategory) {
            params.append('orgCategory', businessParam.orgCategory)
        }
        if (businessParam.parentId) {
            params.append('parentId', businessParam.parentId)
        }
        if (businessParam.orgPathId) {
            params.append('orgPathId', businessParam.orgPathId)
        }
        if (businessParam.orgPathCn) {
            params.append('orgPathCn', businessParam.orgPathCn)
        }
        if (businessParam.description) {
            params.append('description', businessParam.description)
        }
        if (businessParam.treeType) {
            params.append('treeType', businessParam.treeType)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
